#!/bin/bash

# *********************************************
# @author: lsq
# @date: 2021/07/20
# @modify: 2023/12/04
# @function: Operation Db easily by myself!
# *********************************************


MYSQL_PORT=16603
#MYSQL_PORT=16604
REDIS_PORT=16605
#REDIS_PORT=16606

function checkUser()
{
    username=`whoami`
    if [[ ! "${username}"x == "dbuser"x ]];then
        echo "[ERROR] pls use dbuser to start|stop app!"
        return 1
    fi
}

function usage()
{
    echo "usage: bash DbUtil.sh DBType ActionName"
    echo "[login mysql] mysql -uroot -P16603 -h127.0.0.1"
    echo "[login redis] /cloud/db/redis/redis_bin/bin/redis-cli -a redis2030 -p 16605"
}

function startMysql()
{
    echo "Start Mysql now!"
    mysqld_multi --defaults-file=/etc/my.cnf --log=/cloud/db/mysql/mysqld_multi.log start ${MYSQL_PORT}
    sleep 3
    lsof -i:${MYSQL_PORT} > /dev/null 2>&1
    if [[ $? -eq 0 ]];then
        echo "[INFO] Start mysql success!"
    else
        echo "[ERROR] Start mysql failed!"
    fi
}

function startRedis()
{
    echo "Start Redis now!"
}

function startPostgreSQL()
{
    echo "Start PostgreSQL now!"
}

function startMongoDB()
{
    echo "Start MongoDB now!"
}


function stopMysql()
{
    echo "Stop Mysql now!"
    mysqld_multi --defaults-file=/etc/my.cnf --log=/cloud/db/mysql/mysqld_multi.log stop ${MYSQL_PORT}
    sleep 3
    lsof -i:${MYSQL_PORT} > /dev/null 2>&1
    if [[ $? -eq 0 ]];then
        echo "[ERROR] Stop mysql failed!"
	mysqlPid=`lsof -i:${MYSQL_PORT} | grep mysqld | awk '{print $2}'`
	kill -9 ${mysqlPid}
        lsof -i:${MYSQL_PORT} > /dev/null 2>&1
        if [[ $? -eq 0 ]];then
            echo "[ERROR] Stop mysql failed again!"
	else
            echo "[INFO] Stop mysql success!"
	fi
    else
        echo "[INFO] Stop mysql success!"
    fi

}

function stopRedis()
{
    echo "Stop Mysql now!"
}

function stopPostgreSQL()
{
    echo "Stop PostgreSQL now!"
}

function stopMongoDB()
{
    echo "Stop MongoDB now!"
}

function statusMysql()
{
    echo "Check Mysql status now!"
    mysqld_multi --defaults-file=/etc/my.cnf report
}

function statusRedis()
{
    echo "Check Redis status now!"
}

function statusPostgreSQL()
{
    echo "Check PostgreSQL status now!"
}

function statusMongoDB()
{
    echo "Check MongoDB status now!"
}

function fn_main()
{  
    if [[ ! $# -eq 2 ]];
    then
        usage
        exit
    fi

    appName=$1
    appAction=$2

    checkUser || return $?
    if [[ "${appName}"x == "redis"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startRedis || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopRedis || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartRedis || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusRedis || return $?
        fi
    fi 
    
    if [[ "${appName}"x == "mysql"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startMysql || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopMysql || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartMysql || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusMysql || return $?
        fi
    fi 

    if [[ "${appName}"x == "postgresql"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startPostgreSQL || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopPostgreSQL || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartPostgreSQL || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusPostgreSQL || return $?
        fi
    fi 

    if [[ "${appName}"x == "mongodb"x ]];then
        if [[ "${appAction}"x == "start"x ]];then
            startMongoDB || return $?
        elif [[ "${appAction}"x == "stop"x ]];then
            stopMongoDB || return $?
        elif [[ "${appAction}"x == "restart"x ]];then
            restartMongoDB || return $?
        elif [[ "${appAction}"x == "status"x ]];then
            statusMongoDB || return $?
        fi
    fi 

}

fn_main "$@"
result=$?

if [[ "$result" -eq 0 ]]
then
    echo "SUCCESS"
else
    echo "FAILED"
fi
